﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using SharedGamesClasses;

namespace GuiGames
{
    /// <summary>
    /// Provides a GUI for the Crazy Eights game in the SharedGamesClasses.
    /// </summary>
    public partial class CrazyEightsForm : Form
    {
       public Games MainGame;
        public CrazyEightsForm(Games _game)
        {
            InitializeComponent();
            Trace.Listeners.Add(new ListBoxTraceListener(crazyEightsListBox));
            this.MainGame = _game;
            InitForm();
        }

        private void InitForm()
        {
            btnSortCards.Enabled = false;
            

        }

        protected override void OnClosed(EventArgs e)
        {
            MainGame.Show();
        }

        private void btnCancelGame_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSortCards_Click(object sender, EventArgs e)
        {
          Hand hand = CrazyEights.GetHand(CrazyEights.USER);
          hand.Sort();
          DisplayGuiHand(hand, tlpPlayer, CrazyEights.USER);
        }

        private void btnDeal_Click(object sender, EventArgs e)
        {
            CrazyEights.Deal();
            DisplayGuiHand(CrazyEights.GetHand(CrazyEights.USER), tlpPlayer, CrazyEights.USER);
            DisplayGuiHand(CrazyEights.GetHand(CrazyEights.COMPUTER), tlpComputer, CrazyEights.COMPUTER);
            Card card = CrazyEights.GetTopCardOnDiscardPile();
            ChangeDiscardPileImage(card);
            btnDeal.Enabled = false;
            btnSortCards.Enabled = true;
        }

        public void DisplayGuiHand(Hand hand, TableLayoutPanel tableLayoutPanel, int person)
        {

            tableLayoutPanel.Controls.Clear();  // Remove any cards already being shown.

            foreach (Card card in hand)
            {

                // Construct a PictureBox object.
                PictureBox pictureBox = new PictureBox();
                // Tell the PictureBox to use all the space inside its square.
                pictureBox.Dock = DockStyle.Fill;
                // Remove spacing around the PictureBox. (Default is 3 pixels.)
                pictureBox.Margin = new Padding(0);

                pictureBox.Image = Images.GetCardImage(card);
                if (person == CrazyEights.USER) {
                    pictureBox.Click += new EventHandler(pictureBox_Click);
                    pictureBox.Tag = card;
                }

                // Add the PictureBox object to the tableLayoutPanel.
                tableLayoutPanel.Controls.Add(pictureBox);
            }
        }
        public void pictureBox_Click(object sender, EventArgs e)
        {
            // Which card was clicked?
            PictureBox clickedPictureBox = (PictureBox)sender;
            Card clickedCard = (Card)clickedPictureBox.Tag;
            DrawCard(clickedCard);
        }

        

        public void ChangeDiscardPileImage(Card card)
        {
            
            ptbDiscardPile.Image = Images.GetCardImage(card);

        }
        public void DrawCard(Card card)
        {
            switch (CrazyEights.DrawACard(card))
            {
                case 0:
                    {
                        ChangeDiscardPileImage(card);
                        DisplayGuiHand(CrazyEights.GetHand(CrazyEights.USER), tlpPlayer, CrazyEights.USER);
                        ComputerDrawCard();
                        DisplayGuiHand(CrazyEights.GetHand(CrazyEights.COMPUTER), tlpComputer, CrazyEights.COMPUTER);
                        ChangeDiscardPileImage(CrazyEights.GetTopCardOnDiscardPile());
                    }
                    break;
                case 1:
                    {
                        ChooseSuitForm choose = new ChooseSuitForm(this, card);
                        choose.Show();
                        this.Hide();
                    }
                    break;
                case -1:
                    break;
            }
            
        }

        public void ContinueDrawCard(Card card)
        {
            ChangeDiscardPileImage(card);
            DisplayGuiHand(CrazyEights.GetHand(CrazyEights.USER), tlpPlayer, CrazyEights.USER);
            ComputerDrawCard();
            DisplayGuiHand(CrazyEights.GetHand(CrazyEights.COMPUTER), tlpComputer, CrazyEights.COMPUTER);
        }

        public void ComputerDrawCard()
        {
            CrazyEights.ComputerDrawCard();
        }

      
    }
}
